package com.zyj.exoplayerdemo

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.google.android.exoplayer2.upstream.DataSpec
import com.google.android.exoplayer2.upstream.cache.CacheWriter
import com.zyj.exoplayerdemo.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        //播放音频
        binding.MP3.setOnClickListener {
            startActivity(Intent(this@MainActivity, PlayMp3Activity::class.java))
        }

        //播放视频
        binding.MP4.setOnClickListener {
            startActivity(Intent(this@MainActivity, PlayMp4Activity::class.java))
        }

        //视频缓存
        binding.videoCache.setOnClickListener {

            val progressListener = CacheWriter.ProgressListener { requestLength, bytesCached, _ ->
                val downloadPercentage: Double = (bytesCached * 100.0 / requestLength)
                // Do Something
                Log.d("zyj-", "progressListener $downloadPercentage")
            }

            val videoUri = Uri.parse("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")
            val dataSpec = DataSpec(videoUri)

            VideoCacheUtil.cacheoAsync(this, dataSpec, progressListener)
        }
    }
}